##########
#Playing to the Gallery: Emotive Rhetoric in Parliaments
#Moritz Osnabruegge, Sara B. Hobolt, Toni Rodon
#This script produces Figure A1
##########


rm(list=ls())

library("readxl")
library("gdata")
library("magrittr")
library("ggplot2")
library("plyr")

#Specify the path here
setwd("")

#Import media dataset
data = read_excel("commons_stats.xlsx", sheet="commons")

#Extract date from column Date
data$date = format(as.POSIXct(data$Date,format='%Y-%m-%d %H:%M:%S'),format='%Y-%m-%d')
data$year = format(as.POSIXct(data$Date,format='%Y-%m-%d %H:%M:%S'),format='%Y')

data = subset(data, date>=as.Date("2015-04-01"))

data$weekday = weekdays(as.Date(data$date))

data$day = "Other"
data$day[data$weekday=="Wednesday"] = "Wednesday"
data$total_viewers = as.numeric(data$total_viewers)

other_days = subset(data, day=="Other")
summary(other_days$total_viewers)

wednesday = subset(data, day=="Wednesday")
summary(wednesday$total_viewers)


#Figure A1: Number of viewers by day (first, second and third quartiles)
result = data.frame(day=c("Other","Wednesday"), min=c(2969,7394), low=c(2969,7394), 
         mid=c(4382,10328), up=c(7574,15465), max=c(7574,15465))

p = ggplot(result, aes(x=day, min=min, lower=low, middle=mid, upper=up, max=max)) +
    geom_boxplot(stat="identity") + ylim(0,16000) +  
    ylab("Number of Viewers per Day") + xlab("Day") +
    theme(axis.text.x=element_text(size=18),
    axis.text.y=element_text(size=18),  
    axis.title.x=element_text(size=20),
    axis.title.y=element_text(size=20))

pdf("figure_a1.pdf",width=15,height=8)
print(p)
dev.off()
